//
//  SignInRecordViewController.m
//  Estate
//
//  Created by wangdongdong on 2016/11/24.
//  Copyright © 2016年 ttsx. All rights reserved.
//

#import "SignInRecordViewController.h"
#import "indexTableCell.h"
#import "QianDViewController.h"
#import "SegmentViewController.h"
#import "TTSignInRecordYesViewController.h"
#import "TTSignInRecordNoViewController.h"
#import "LYEmptyViewHeader.h"

@interface SignInRecordViewController ()<UITableViewDelegate, UITableViewDataSource>

@property(strong, nonatomic)UITableView *tableView;
@property (strong, nonatomic)NSMutableArray *dataArray;

@end


@implementation SignInRecordViewController

-(NSMutableArray *)dataArray{
    if(!_dataArray){
        _dataArray=[NSMutableArray array];
    }
    return _dataArray;
}

-(UITableView *)tableView {
    if (_tableView == nil) {
        CGRect rect = CGRectMake(0, 10, self.view.frame.size.width, self.view.frame.size.height);
        _tableView = [[UITableView alloc] initWithFrame:rect];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1.00];
        [_tableView registerNib:[UINib nibWithNibName:@"indexTableCell" bundle:nil] forCellReuseIdentifier:@"index"];
        _tableView.tableFooterView = [[UIView alloc]init];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"签到记录";
    // 获取当前角色
    NSString *role=DEF_PERSISTENT_GET_OBJECT(@"role");
    if ([role isEqualToString:@"USER_01"] || [role isEqualToString:@"USER_07"]) {
        //总监
        SegmentViewController *vc = [[SegmentViewController alloc]init];
        vc.height = DEF_SCREEN_HEIGHT - 108;
        vc.titleArray =@[@"已考勤信息",@"未考勤信息"];
        TTSignInRecordYesViewController *basic=[[TTSignInRecordYesViewController alloc]init];
        TTSignInRecordNoViewController *basic1=[[TTSignInRecordNoViewController alloc]init];
        NSMutableArray *controlArray = [[NSMutableArray alloc]initWithObjects:basic,basic1,nil];
        //设置选中字体颜色
        vc.titleSelectedColor = [TTTheme defaultManger].defaultColor;
        //设置子视图
        vc.subViewControllers = controlArray;
        //标签栏背景色
        vc.headViewBackgroundColor=[UIColor colorWithRed:0.98 green:0.98 blue:0.98 alpha:1.00];
        //非选中状态下标签字体大小
        vc.fontSize=14.;
        //选中下划线颜色
        vc.bottomLineColor=[TTTheme defaultManger].defaultColor;
        vc.buttonWidth = DEF_SCREEN_WIDTH/2;
        vc.buttonHeight = 40.;
        [vc initSegment];
        [vc addParentController:self];
    }else{
        
        [self.view addSubview:self.tableView];
        LYEmptyView *emptyView = [LYEmptyView emptyViewWithImageStr:@"ic_empty"
                                  
                                                           titleStr:@"暂无数据"
                                  
                                                          detailStr:@""];
        //元素竖直方向的间距
        emptyView.imageSize = CGSizeMake(150, 150);
        emptyView.contentViewY = 120.f;
        self.tableView.ly_emptyView = emptyView;
        [self operateDataBase];
        self.view.backgroundColor = [UIColor whiteColor];
    }
  
}

#pragma mark-数据库操作
- (void)operateDataBase {
    
    /*
     ** 此处万和和诺士诚不同，万和查询项目下所有签到退人员，诺士诚只查询自己。
     */
    
    FMDatabase *db = [FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    [db open];
    [self.dataArray removeAllObjects];
    
    // 数据库操作 签到签退查看
     NSString *sqlStr=@"";
    if([USERROLE isEqualToString:@"USER_01"]){
      sqlStr= [NSString stringWithFormat:@"SELECT * FROM WORK_RECORD WHERE RECORD_TYPE IN('RECORD_05','RECORD_06') AND PRO_ID='%@' ORDER by CREATE_TIME DESC",PROID];
    }else{
        sqlStr=SQL_WORKRECORD_SELECT;
    }
    
    FMResultSet *resultSet = [db executeQuery:sqlStr];
    
    // 遍历结果
    while ([resultSet next]) {
        
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        
        // 用户id
        NSString *id = [resultSet stringForColumn:@"RECORD_USER"];
        // 类型
        NSString *type = [resultSet stringForColumn:@"RECORD_TYPE"];
        [dic setObject:type forKey:@"type"];
        
        // 用户
        NSString *name = [self userById:id];
        [dic setObject:DEFISNUL(name) forKey:@"name"];
        
        // 拼接字符串
        [dic setObject:[resultSet stringForColumn:@"RECORD_TITLE"] forKey:@"cellText"];
        // 时间
        NSString *time=[resultSet stringForColumn:@"CREATE_TIME"];
        [dic setObject:time forKey:@"time"];
        
        // 附件id
        NSString *fileId = [resultSet stringForColumn:@"RECORD_FILE_PATH"];
        fileId = [LSUtility stringWithString:fileId];
        NSArray *arrs = [LSUtility dictionaryWithJsonString:fileId];
        NSDictionary *fileDic = [arrs firstObject];
        [dic setObject:fileDic[@"fileId"] forKey:@"fileId"];
        
        // 地区 天气
        NSString *addAndWea=[resultSet stringForColumn:@"WEATHER"];
        NSArray *arr=[addAndWea componentsSeparatedByString:@"/"];
        // 天气
        [dic setObject:DEFISNUL(arr[0]) forKey:@"address"];
        
        // 地区
        [dic setObject:DEFISNUL([arr lastObject]) forKey:@"weather"];
        // 记录地点
        NSString *addInfo=[resultSet stringForColumn:@"ADDRES_INFO"];
        [dic setObject:DEFISNUL(addInfo) forKey:@"addInfo"];
        
        // 添加到数组
        [self.dataArray addObject:dic];
    }
    [self.tableView reloadData];
}

//查询用户
- (NSString *)userById:(NSString *)id
{
    FMDatabase *db = [FMDatabase databaseWithPath:DEF_DB_DATABASEPATH];
    [db open];
    NSString *sql=[NSString stringWithFormat:@"SELECT * FROM USER WHERE ID='%@'",id];
    FMResultSet *resultSet = [db executeQuery:sql];
    NSString *name;
    while ([resultSet next]) {
        name = [resultSet stringForColumn:@"REAL_NAME"];
    }
    return name;
}

#pragma mark-UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    indexTableCell *cell=[tableView dequeueReusableCellWithIdentifier:@"index" forIndexPath:indexPath];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    //设置文字
    NSDictionary *dict = self.dataArray[indexPath.row];
    cell.qiandaoUser.text=dict[@"cellText"];
    
    //判断签到 签退
    if([dict[@"type"] isEqualToString:@"RECORD_05"]){
        cell.line.backgroundColor=[UIColor colorWithRed:0.68 green:0.36 blue:0.63 alpha:1.00];
    }else{
        cell.line.backgroundColor=[UIColor colorWithRed:0.00 green:0.59 blue:0.53 alpha:1.00];
    }
    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    //设置时间
    cell.qiandaoDate.text = dict[@"time"];
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dict = self.dataArray[indexPath.row];
    QianDViewController *qiand = [[QianDViewController alloc]init];
    qiand.dict = dict;
    [self.navigationController pushViewController:qiand animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end


